package com.sxfq.loansupermarket.backsystem.modular.tcCenter.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.ReturnVisitInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.SearchReturnVisitInfo;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.TcInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.TcRevisitInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.IConsultantDiagnosisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/diagnosis")
public class ConsultantDiagnosisController extends BaseController {
    @Autowired
    IConsultantDiagnosisService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/tcCenter/consultant/";

    /**
     * 跳转到回访记录
     */
    @RequestMapping("")
    public String consultantRecord(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getTcList() == null) {
            String getListSql = " select a.id as id,a.account as tcName from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                    " where b.abbreviation='"+tableHead+"' and c.name like CONCAT('%','tc','%') ";
            List<TcInfo> tcList =sqlMapper.selectList(getListSql,TcInfo.class);
            ShiroKit.getUser().setTcList(tcList);
        }
        model.addAttribute("tcList",  ShiroKit.getUser().getTcList());
        model.addAttribute("outpatientManagementList", ShiroKit.getSessionAttr("outpatientManagementList"));
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "consultant_diagnosis.html";
    }

    /**
     * 看诊记录
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object consultantInfoList(SearchReturnVisitInfo info, HttpServletRequest request) {
        if(info.getAbbreviation()==""|| info.getAbbreviation()==null || info.getAbbreviation() =="null"){
            return null;
        }else{
            Page<ReturnVisitInfo> page = new PageFactory<ReturnVisitInfo>().defaultPage();
            try {
                List<ReturnVisitInfo> list = new ArrayList<ReturnVisitInfo>();
                list = service.selectPageConsultantByQuery(page, info);
                if (list.size() == 0) {
                    page.setCurrent(1);
                    list = service.selectPageConsultantByQuery(page, info);
                }
                page.setTotal(service.getQueryPagesCount(page, info));
                page.setRecords(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return super.packForBT(page);
        }
    }

    /**
     * 删除看诊
     */
    @RequestMapping(value = "/delete_tcDiagnosis")
    @ResponseBody
    public int delete_tcDiagnosis(String id,String reservationsId, Model model) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "delete from tc_diagnosis_info  where id = '" + id + "'";
        sqlMapper.delete(sql);
        String sql1="select * from tc_diagnosis_info where reservations_id='"+reservationsId+"';";
        List<CustomerInfo> list=sqlMapper.selectList(sql1,CustomerInfo.class);
        if(list.size()>0) {
            String sql2 = "update " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " set diagnosis_status='已看诊' where reservations_id='" + reservationsId + "'";
            return sqlMapper.update(sql2);
        }else {
            String sql2 = "update " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " set diagnosis_status='未看诊' where reservations_id='" + reservationsId + "'";
            return sqlMapper.update(sql2);
        }
    }

    /**
     * 编辑看诊
     */
    @RequestMapping(value = "/update_tcDiagnosis/{id}")
    public String update_tcDiagnosis(@PathVariable String id, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String sql = "select id,diagnosis_content from tc_diagnosis_info where id ='"+id+"'";
        TcRevisitInfo tcRevisitInfo = sqlMapper.selectOne(sql,TcRevisitInfo.class);
        model.addAttribute("id", id);
        model.addAttribute("diagnosisContent", tcRevisitInfo.getDiagnosisContent());
        return PREFIX + "update_tcDiagnosis.html";
    }


    //编辑看诊
    @RequestMapping(value = "/update_tcDiagnosisOk", method = RequestMethod.POST)
    @ResponseBody
    public int update_tcDiagnosisOk(String id, String diagnosisContent ) throws Exception {
        String sql = "update tc_diagnosis_info set diagnosis_content='" + diagnosisContent + "'" +
                " where id='" + id + "' ";
        return sqlMapper.update(sql);
    }
}